package gov.nist.ssd.swqb.config;

import gov.nist.ssd.swqb.core.files.OptionManager;

public class DebugMode {

    private static final String OPTION_STRING = "gov.nist.ssd.swqb.DEBUG_MODE";
    private static boolean alreadyAccessedDebugModeStatus = false;
    private static boolean debugModeStatus;
    private static OptionManager optionManager;

    private DebugMode() {;
    }

    public static void setOptionManager(OptionManager o) {
        optionManager = o;
    }

    public static boolean debugModeIsOn() {
        if (alreadyAccessedDebugModeStatus) {
            return debugModeStatus;
        } else {
            String debugMode = System.getProperty(OPTION_STRING);

            if (debugMode == null) {

                if (optionManager == null) {
                    debugModeStatus = false;
                } else {
                    String debug = optionManager.getOption("debug_mode");
                    if (debug != null && debug.equals("1")) {
                        debugModeStatus = true;
                    }
                }
            } else if (debugMode.trim().toLowerCase().equals("true")) {
                debugModeStatus = true;
            }

            alreadyAccessedDebugModeStatus = true;
            return debugModeStatus;
        }
    }

    public static void setDebugMode(boolean value) {
        debugModeStatus = value;
        alreadyAccessedDebugModeStatus = true;
    }

    public static void printSparqlDebugInformations(DClassName className,
            DMethodName methodName,
            DRequestGoal requestGoal,
            DSparqlText sparqlText) {

        StringBuffer sb = new StringBuffer();
        sb.append("============================================================\n");
        sb.append("== DEBUG : SPARQL REQUEST ==================================\n");
        sb.append("============================================================\n");
        sb.append("SOURCE :    ").append(className).append('.').append(methodName).append('\n');
        sb.append("OBJECTIVE : ").append(requestGoal).append('\n');
        sb.append("============================================================\n");
        sb.append('\n');
        sb.append(sparqlText);
        sb.append('\n');
        sb.append("============================================================\n");

        System.out.println(sb);
    }

    private static class TextTyper {

        private String text;

        protected TextTyper(String str) {
            this.text = str;
        }

        public String toString() {
            return text;
        }
    }

    public static class DSparqlText extends TextTyper {

        protected DSparqlText(String str) {
            super(str);
        }
    }

    public static DSparqlText dSparqlText(String sparqlText) {
        return new DSparqlText(sparqlText);
    }

    public static class DMethodName extends TextTyper {

        protected DMethodName(String str) {
            super(str);
        }
    }

    public static class DClassName extends TextTyper {

        protected DClassName(String str) {
            super(str);
        }
    }

    public static DClassName dClassName(String className) {
        return new DClassName(className);
    }

    public static DMethodName dMethodName(String methodName) {
        return new DMethodName(methodName);
    }

    public static class DRequestGoal extends TextTyper {

        protected DRequestGoal(String str) {
            super(str);
        }
    }

    public static DRequestGoal dRequestGoal(String requestGoal) {
        return new DRequestGoal(requestGoal);
    }
}
